/**
 * 原创属性设置模块
 */
define( [
  'jquery',
  'require',
  'artCategory',
  'message',
  '../../lib/utils',
  '../../lib/editData',
  'bigwe_utils'
], function( $, require, artCategory ) {

  var bu = require( 'bigwe_utils' );
  var editData = require( '../../lib/editData' );
  var GraphBuild = require( '../../lib/utils' );
  var ShowTitleMessage = require( 'message' );

  var category = artCategory.CATEGORY_NAME;
  var showTime = ShowTitleMessage.showTime;

  var $original = $( '#original' );
  var $originalRight = $( ".original-div" );

  var Original = {
    _selectorHtmlCreate: function( key ) {
      return '<option value="' + key + '">' + category[ key ] + '</option>';
    },
    fullCategorySelect: function() {
      for ( var key in category ) {
        $( '#original-cate-spa', $original ).append( this._selectorHtmlCreate( key ) );
      }
      $( '#original-cate-spa', $original ).selectpicker( {
        size: 8
      } );
      return this;
    },
    getOriginalInfo: function() {
      var bigweFirst = $( 'input[name="bigweFirst"]:checked', $original ).val();
      return {
        // 微知首发
        bigweFirst: bigweFirst,
        // 原文链接
        resourceUrl: $( '#original-resourceUrl-ipt', $original ).val(),
        // 平台名称
        platfName: bigweFirst === '1' ? '微知首发' : $( '#original-platf-ipt', $original ).val(),
        // 原创作者名称
        authorName: $( '#original-author-ipt', $original ).val(),
        // 文章领域
        article_field: $( '#original-cate-spa', $original ).val(),
        // 是否转载
        reprintSet: $( 'input[name="reprintSet"]:checked', $original ).val()
      };
    }
  };
  Original.fullCategorySelect();
  var InputCheck = {
    sourceUrlCheck: function() {
      var $bigweFirst = $( '#original-resourceUrl-ipt', $original ).on( {
        focus: function() {
          $( this ).removeClass( 'checkError' );
        }
      } );
      if ( $( '[name="bigweFirst"]:checked', $original ).val() === '1' ) {
        return true;
      } else {
        if ( !$.trim( $bigweFirst.val() ) ) {
          ShowTitleMessage.showWarning( "非微知首发时请填写原文链接", showTime );
          $bigweFirst.addClass( 'checkError' );
          return false;
        } else {
          if ( bu.isUrlCheck( $bigweFirst.val() ) ) {
            return true;
          } else {
            ShowTitleMessage.showWarning( "请填写一个正确的原文地址", showTime );
            $bigweFirst.addClass( 'checkError' );
            return false;
          }
        }
      }
    },
    originalPlatfCheck: function() {
      var $originalPlatf = $( '#original-platf-ipt', $original ).on( {
        focus: function() {
          $( this ).removeClass( 'checkError' );
        }
      } );
      if ( $( '[name="bigweFirst"]:checked', $original ).val() === '1' ) {
        return true
      } else {
        if ( $originalPlatf.val().length < 2 ) {
          ShowTitleMessage.showWarning( "平台名称请不要少于两个字", showTime );
          $originalPlatf.addClass( 'checkError' );
          return false;
        } else {
          return true;
        }
      }
    },
    authorCheck: function() {
      var $author = $( '#original-author-ipt', $original ).on( {
        focus: function() {
          $( this ).removeClass( 'checkError' );
        }
      } );
      if ( !$.trim( $author.val() ) ) {
        ShowTitleMessage.showWarning( "作者不能为空", showTime );
        $author.addClass( 'checkError' );
        return false;
      } else if ( bu.getStrChartLen( $.trim( $author.val() ) ) > 16 ) {
        ShowTitleMessage.showWarning( "作者长度不能超过8个字", showTime );
        $author.addClass( 'checkError' );
        return false;
      } else {
        return true;
      }
    },
    categoryCheck: function() {
      var $categorySelector = $( '#original-cate-spa', $original );
      $( '.bootstrap-select>.btn', $original ).on( {
        focus: function() {
          $( this ).removeClass( 'checkError' );
        }
      } );
      if ( !$categorySelector.val() ) {
        ShowTitleMessage.showWarning( "请选择一个文章领域", showTime );
        $( '.bootstrap-select>.btn', $original ).addClass( 'checkError' );
        $categorySelector.addClass( 'checkError' );
        return false;
      } else {
        return true;
      }
    }
  };
  // 原创标识下一步操作
  $original.on( 'click', "#original-next", function() {
    $( ".original-main .content", $original ).transit( {
      x: -811
    }, 0.2e3, function() {
      $( ".top-tab>.tab1", $original ).removeClass( "selected" );
      $( ".top-tab>.tab2", $original ).removeClass( "selected-san" );
      $( ".top-tab>.tab4", $original ).addClass( "selected" );
    } );
  } ).on( 'click', "#original-prev", function() { // 原创标识上一步操作
    $( ".original-main .content", $original ).transit( {
      x: 0
    }, 0.2e3, function() {
      $( ".top-tab>.tab1", $original ).addClass( "selected" );
      $( ".top-tab>.tab2", $original ).addClass( "selected-san" );
      $( ".top-tab>.tab4", $original ).removeClass( "selected" );
    } );
  } ).on( 'change', 'input[name="bigweFirst"]', function() { // 微知首发点击
    var $isCheck = $( 'input[name="bigweFirst"]:checked', $original ).val();
    if ( $isCheck === '1' ) {
      $( "#original-platf-ipt", $original ).val( '微知首发' ).attr( 'disabled', 'disabled' );
      $( "#original-resourceUrl-spa", $original ).hide();
    } else {
      $( "#original-platf-ipt", $original ).removeAttr( 'disabled' );
      $( "#original-platf-ipt", $original ).val( '' );
      $( "#original-resourceUrl-spa", $original ).show();
    }
  } ).on( 'click', "#original-sure", function() {
    // 原创声明确定
    var thisOriginal = InputCheck;
    if ( thisOriginal.sourceUrlCheck() && thisOriginal.originalPlatfCheck() && thisOriginal.authorCheck() && thisOriginal.categoryCheck() ) {
      var originalInfo = Original.getOriginalInfo();

      originalInfo.declare_original = 1;
      originalInfo.isAllCheck = 1;
      // 页面设置
      GraphBuild.originalSet( originalInfo );
      // 值设置
      editData.setAttrbute( originalInfo );

      $original.modal( 'hide' );
      /*ShowTitleMessage.show('文章内容验证中', showTime);
      $.post('edit_text/check_original', {
      	content: editorObj.getContentTxt()
      }, function(data) {
      	var jsonData = $.parseJSON(data);
      	if (!jsonData.success) {
      		ShowTitleMessage.showWarning(jsonData.msg, showTime);
      		return;
      	}
      	ShowTitleMessage.hide();
      	$('#original').modal('hide');
      });*/
    }
  } ).on( 'show.bs.modal', function() {
    $( '#original-cate-spa>option' ).removeAttr( 'selected' );
    $( '#original-cate-spa>[value="' + editData.getAttrbute( 'article_field' ) + '"]', this ).attr( 'selected', 'selected' ).parent().selectpicker( 'refresh' );

    if ( !$.trim( $( '#original-author-ipt' ).val() ) ) {
      $( '#original-author-ipt' ).val( $( '.article-author-input .author-input' ).val() )
    }
  } ).on( 'hide.bs.modal', function() {
    $( '.checkError', this ).removeClass( 'checkError' );
    // 用户未填写文章作者时自动补充声明原创时的作者
    var authorName = $.trim( $( '#original-author-ipt', this ).val() );
    var $articleAuthIn = $( '.article-author-input .author-input' );
    var articleAuthor = $.trim( $articleAuthIn.val() );
    if ( authorName && !articleAuthor ) {
      $articleAuthIn.val( authorName ).trigger( 'change' );
    }
  } );

  $originalRight.on( 'click', '#btn-update-declar', function() {
    // 修改声明
    $original.modal( 'show' );
    $( "#original-next" ).click();
  } ).on( 'click', '#btn-cancel-declar', function() {
    // 取消声明
    editData.setAttrbute( 'declare_original' );
    GraphBuild.originalCancel();
  } );

} );
